package com.plian.system.controller.ms;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.TokenUtil;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.common.status.CustomCode;
import com.plian.system.config.Resubmit;
import com.plian.system.constant.sys.FormStatusConstant;
import com.plian.system.entity.ms.IssueSituation;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.search.TokenSearch;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.ms.IIssueSituationService;
import com.plian.system.vo.ms.IssueSituationVO;
import com.plian.system.wrapper.ms.IssueSituationWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * @author huzb
 * @create 2020-11-09 10:50
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("api/issuesituation")
public class IssueSituationController {

    private IIssueSituationService iIssueSituationService;

    private IssueSituationWrapper issueSituationWrapper;

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @Resubmit(delaySeconds = 0)
    @LogOperation("问题情况-提交")
    public ObjResult submit(@Valid @RequestBody IssueSituation issueSituation) {
        if (Optional.ofNullable(issueSituation.getStatus()).isPresent() &&
                issueSituation.getStatus() != FormStatusConstant.STASH){
            if (!iIssueSituationService.checkIsExisted(issueSituation.getId(), issueSituation.getCode())){
                return new ObjResult(CustomCode.builder().message("单据编码已存在").code(1999).success(false).build());
            }
        }
        Integer isRectification = issueSituation.getIsRectification();
        if (isRectification == null){
            issueSituation.setIsRectification(0);
        }
        boolean result = iIssueSituationService.submit(issueSituation);
        return result ? new ObjResult(CommonCode.SUCCESS) : new ObjResult(CommonCode.FAIL);
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @LogOperation("问题情况-删除")
    public ObjResult remove(@RequestBody PageData pd) {
        List<String> ids = (List<String>)pd.get("ids");
        return iIssueSituationService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
                ? new ObjResult(CommonCode.SUCCESS) : new ObjResult(CommonCode.FAIL);
    }

    /**
     * 详情
     */
    @PostMapping("/detail")
    public R<IssueSituationVO> detail(@Valid @RequestBody IssueSituation issueSituation) {
        IssueSituation detail = iIssueSituationService.getOne(Condition.getQueryWrapper(issueSituation));
        R<IssueSituationVO> objResult = new R<>(CommonCode.SUCCESS);
        IssueSituationVO issueSituationVO = issueSituationWrapper.entityToVO(detail);
        objResult.setPageData(issueSituationVO);
        return objResult;
    }

    /**
     * 分页
     */
    @PostMapping("/list")
    public ListResult<IssueSituationVO> list(@RequestBody Query query) {
        List<SearchEntity> entityList = query.getSearch();
        if (query.getAscs()==null&&query.getDescs()==null){
            query.setDescs("create_time");
        }
        QueryWrapper<IssueSituation> queryWrapper = SearchParamEntiy.getQueryWrapper(
                Condition.getQueryWrapper(new IssueSituation()), entityList
        );
        TokenSearch<IssueSituation> tokenSearch = new TokenSearch<>();
		tokenSearch.addTokenQuery(queryWrapper, query);
        IPage<IssueSituation> pages = iIssueSituationService.page(Condition.getPage(query), queryWrapper);
        List<IssueSituationVO> issueSituationVOList = issueSituationWrapper.entityToVO(pages.getRecords());
        ListResult<IssueSituationVO> listResult = new ListResult<>(CommonCode.SUCCESS);
        listResult.setList(issueSituationVOList);
        listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
        return listResult;
    }

    /**
     * 未整改问题列表
     */
    @PostMapping("/issuelist")
    public ListResult<IssueSituation> issuelist(){
        String orgId = TokenUtil.getTokenOrgId();
        List<IssueSituation> issuelist = iIssueSituationService.issuelist(orgId);
        ListResult<IssueSituation> listResult = new ListResult<>(CommonCode.SUCCESS);
        listResult.setList(issuelist);
        listResult.setTotal(issuelist.size());
        return listResult;
    }

    /**
     * 处室管理界面报表
     */
    @PostMapping("/orgsForNum")
    @Resubmit(delaySeconds = 0)
    public ListResult orgsForNum(@RequestBody PageData pageData) {
        List<PageData> pageDataList = iIssueSituationService.orgsForNum(pageData);
        ListResult<PageData> listResult = new ListResult<>(CommonCode.SUCCESS);
        listResult.setList(pageDataList);
        listResult.setTotal(pageDataList.size());
        return listResult;
    }

//    /** TODO DEL
//     * 月度excel导出
//     * @param
//     * @return
//     */
//    @PostMapping(value = "/exportExcelByMonth", produces = "application/json;charset=UTF-8")
//    public void exportExcelByMonth(@RequestBody List<PageData> pdlist, HttpServletResponse response, OutputStream os){
//        iIssueSituationService.exportExcelByMonth(pdlist,response,os);
//    }

//    /**
//     * 月度excel导出
//     * @param
//     * @return
//     */
//    @PostMapping(value = "/exportExcelByYear", produces = "application/json;charset=UTF-8")
//    public void exportExcelByYear(@RequestBody List<PageData> pdlist, HttpServletResponse response, OutputStream os){
//        iIssueSituationService.exportExcelByYear(pdlist,response,os);
//    }
//
//    @PostMapping(value = "/exportExcelByRE", produces = "application/json;charset=UTF-8")
//    public void exportExcelByRE(@RequestBody List<PageData> pdlist, HttpServletResponse response, OutputStream os){
//        iIssueSituationService.exportExcelByRE(pdlist,response,os);
//    }
}
